package pl.edu.uj.ii.battleships.adapters;

import java.util.ArrayList;

import pl.edu.uj.ii.battleships.R;
import pl.edu.uj.ii.battleships.models.BoardItem;
import pl.edu.uj.ii.battleships.models.Ship;
import pl.edu.uj.ii.battleships.models.BoardItem.Status;
import pl.edu.uj.ii.battleships.models.Ship.Type;
import pl.edu.uj.ii.battleships.utils.GameDataManager;
import pl.edu.uj.ii.battleships.utils.Logger;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class GridAdapter extends BaseAdapter {

  private int numberOfItems = 100;
  //private Bitmap[] bitmap ;
  private BoardItem[] items; 
  private ArrayList<Integer> lockedPositions = new ArrayList<Integer>();
  
  private Context context;
  private LayoutInflater layoutInflater;
   
  public GridAdapter(Context c) {
	  context = c;
	  layoutInflater = LayoutInflater.from(context);
	  numberOfItems = GameDataManager.getBoardHeight() * GameDataManager.getBoardWidth();
	  items =  new BoardItem[numberOfItems]; 

	  for(int i = 0; i < numberOfItems; i++) {
		  items[i] = new BoardItem(context);
		  items[i].setBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.empty));
		  items[i].setStatus(Status.EMPTY);
	  }
  }
 
  public int getCount() {
	  return items.length;
  }
 
  public BoardItem getItem(int position) {
	  return items[position];
  }
 
  public long getItemId(int position) {
	  return position;
  }
  
  public void setItem(int position, Bitmap bmap){
	  items[position].setBitmap(bmap);
  }
  
  public void setStatus(int position, Status s) {
	  items[position].setStatus(s);
  }
  
  public Ship placeShip(int position, Type type, boolean placeDown) {
	  Ship ship = new Ship(type);
	 
		  ArrayList<Integer> positions = new ArrayList<Integer>();
		  int boarditems = 0;
		  if(type.equals(Type.FIVEMAST)) {
			  boarditems = 5;
		  } else if (type.equals(Type.FOURMAST)) {
			  boarditems = 4;
		  } else if (type.equals(Type.THREEMAST)) {
			  boarditems = 3;
		  } else if (type.equals(Type.TWOMAST)) {
			  boarditems = 2;
		  }
			  
		  
		  if(placeDown) { 
			  //validate position
			  int pos = position;
			  
			  for(int i = 0; i < boarditems; i++) {
				  if(pos >= items.length)
					  return null;
				  if(lockedPositions.contains(pos))
					  return null;
				  
				  pos += GameDataManager.getBoardWidth();
			  }
			  
			  pos = position;
			  for(int i = 0; i < boarditems; i++) {
				  positions.add(pos);
				  items[pos].setStatus(Status.SHIP);
				  pos += GameDataManager.getBoardWidth();
				//TODO - calculate locked positions
			  }
		  } else {
			  //validate position
			  int level = position / GameDataManager.getBoardWidth();
			  Logger.debug("Position: " + position + "  Level: " + level);

			  for(int i = 0; i < boarditems; i++) {
				  int tmp = position + i;
				  
				  if(level != tmp/GameDataManager.getBoardWidth()) {
					  Logger.debug("Position " + tmp + " is in the wrong level.");
					  return null;
				  }
				  
				  if(lockedPositions.contains(tmp))
					  return null;
			  }
			  
			  for(int i = 0; i < boarditems; i++) {
				  positions.add(position + i);
				  items[position + i].setStatus(Status.SHIP);
				  
				  //TODO - calculate locked positions
			  }
		  }
		  ship.setPositions(positions);
		  ship.setPlacedOnBoard(true);
	  
	  return ship;
  }
 
  public View getView(int position, View convertView, ViewGroup parent) {
	   View grid;
	   if(convertView == null) {
		   grid = new View(context);
		   grid = layoutInflater.inflate(R.layout.grid_layout, null); 
	   } else 
		   grid = (View)convertView; 
	   
	    
	   ImageView imageView = (ImageView)grid.findViewById(R.id.imagepart);
	   imageView.setImageBitmap(items[position].getBitmap());
    
	   return grid;
  }
}
